package com.example.su_shi_tan_app.scan

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.example.library_common.utils.normalShortToast
import com.example.su_shi_tan_app.R
import com.example.su_shi_tan_app.databinding.ScanFragmentBinding
import com.example.su_shi_tan_bas_lib.album.Album
import com.example.su_shi_tan_bas_lib.base.BaseFragmentEx
import com.example.su_shi_tan_bas_lib.base.BaseLazyDataBindingFragment

import com.example.su_shi_tan_core.scan.ScanResListener
import com.google.zxing.Result
import me.dm7.barcodescanner.zxing.ZXingScannerView

class ScanFragment:BaseLazyDataBindingFragment<ScanFragmentBinding>(), View.OnClickListener,
    ZXingScannerView.ResultHandler {

    companion object{
        fun newInstance(): ScanFragment {
            val args = Bundle()
            val fragment = ScanFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var startCamera=false

    private var mScanResListener: ScanResListener? = null

    fun setScanResListener(mScanResListener: ScanResListener?) {
        this.mScanResListener = mScanResListener
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }

    override fun layoutId(): Int {
        return R.layout.scan_fragment
    }


    override fun lazyInit() {
        startCamera=true
        viewBinder.mJssScanView.setAutoFocus(true)
        viewBinder.mJssScanView.setResultHandler(this)
        viewBinder.mJssScanView.startCamera()
    }

    override fun onResume() {
        super.onResume()
        if (!startCamera){
            viewBinder.mJssScanView.startCamera()
        }

    }

    override fun onPause() {
        super.onPause()
        startCamera=false
        viewBinder.mJssScanView.stopCameraPreview()
        viewBinder.mJssScanView.stopCamera()
    }

    override fun initView(view: View) {
        viewBinder.onViewClick=this
    }

    private fun getPicturePath() {
        Album.getInstance()
            .singleImage()
            .onFailed { _, msg ->
                normalShortToast(_mActivity,msg)

            }
            .onSuccess {
                if (it.isEmpty()) {
                    return@onSuccess
                }
                val albumFile = it[0]
                try {
                    parserImg(albumFile.path)
                }catch (e:Exception){
                    e.printStackTrace()
                    normalShortToast(_mActivity,R.string.invaile_q_coding)

                }

            }.execute(childFragmentManager)
    }

    private fun parserImg(picturePath: String?) {
        if (TextUtils.isEmpty(picturePath)) {
            normalShortToast(_mActivity,R.string.invaile_q_coding)

            return
        }

        val str: String = QRCodeDecoder.syncDecodeQRCode("$picturePath")
        if (str.isNullOrEmpty()) {
            Toast.makeText(
                _mActivity,
                _mActivity.getString(R.string.invaile_q_coding),
                Toast.LENGTH_LONG
            ).show()
        } else {
            mScanResListener?.onResStr(str)

            onBackPressedSupport()
        }
    }

    override fun onBackPressedSupport(): Boolean {
        val parentFragment = parentFragment
        if (parentFragment is BaseFragmentEx){
            parentFragment.pop()
            return true
        }
        return super.onBackPressedSupport()
    }

    override fun onDestroy() {
        super.onDestroy()
        mScanResListener=null
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.finish->{
                onBackPressedSupport()
            }
            R.id.title_text_view->{
                getPicturePath()
            }
        }
    }

    override fun handleResult(rawResult: Result?) {
        val text = rawResult?.text ?: ""
        if (text.isEmpty()) {
            normalShortToast(_mActivity,R.string.invaile_q_coding)

        } else {
            mScanResListener?.onResStr(text)

            onBackPressedSupport()
        }

    }


}